package com.springcloud.controller;

import com.springcloud.commons.base.BaseController;
import com.springcloud.commons.result.Result;
import com.springcloud.commons.utils.StringUtils;
import com.springcloud.jwt.JwtConfigProperty;
import com.springcloud.jwt.JwtUtil;
import com.springcloud.model.SysResource;
import com.springcloud.model.SysUser;
import com.springcloud.service.ISysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @description：资源管理
 * @author：wangpeng
 * @date：2015/10/1 14:51
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {

    @Autowired
    private JwtConfigProperty jwtConfig;
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private ISysResourceService resourceService;

    /**
     * 菜单树
     *
     * @return
     */
    @PostMapping("/tree")
    @ResponseBody
    public Object tree(HttpServletRequest request) {
//        String userid = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        SysUser user = new SysUser();
        String authHeader = "";
        Cookie[] cookies = request.getCookies();
        if (cookies!=null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookie.getName().equals(jwtConfig.getHeader())) {
                    authHeader = cookie.getValue();
                    break;
                }
            }
        }
        if(StringUtils.isBlank(authHeader)) {
            authHeader = request.getHeader(jwtConfig.getHeader());//获取浏览器传过来的token信息
        }
        if(StringUtils.isBlank(authHeader)){
            Result result = new Result();
            result.setMsg("请登录！");
            result.setCode(400);
            return result;
        }
        String tokenHead = jwtConfig.getTokenHead(); //获取配置文件的token开头
        String authToken = authHeader.substring(tokenHead.length());//去掉开头
        String userid = jwtUtil.getSubjectFromToken(authToken); // 获取userid
        SysUser user = new SysUser();
        user.setId(Long.parseLong(userid));
        return resourceService.selectTree(user);
    }

    /**
     * 资源管理页
     *
     * @return
     */
    @GetMapping("/manager")
    public String manager() {
        return "admin/resource/resource";
    }

    /**
     * 资源管理列表
     *
     * @return
     */
    @PostMapping("/treeGrid")
    @ResponseBody
    public Object treeGrid() {
        return resourceService.selectAll();
    }

    /**
     * 添加资源页
     *
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/resource/resourceAdd";
    }

    /**
     * 添加资源
     *
     * @param resource
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(SysResource resource) {
        resourceService.insert(resource);
        return renderSuccess("添加成功！");
    }

    /**
     * 二级资源树
     *
     * @return
     */
    @RequestMapping("/allTree")
    @ResponseBody
    public Object allTree() {
        return resourceService.selectAllTree();
    }

    /**
     * 三级资源树
     *
     * @return
     */
    @PostMapping("/allTrees")
    @ResponseBody
    public Object allTrees() {
        return resourceService.selectAllTrees();
    }

    /**
     * 编辑资源页
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model, Long id) {
    	SysResource resource = resourceService.selectById(id);
        model.addAttribute("resource", resource);
        return "admin/resource/resourceEdit";
    }

    /**
     * 编辑资源
     *
     * @param resource
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(SysResource resource) {
        resourceService.updateById(resource);
        return renderSuccess("编辑成功！");
    }

    /**
     * 删除资源
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id) {
        resourceService.deleteById(id);
        return renderSuccess("删除成功！");
    }

}
